﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda IHistoryList</summary>
	public class IHistoryList : IDisposable
	{
        public int Capacity; // Kapacita
        protected System.Collections.Generic.List<string> FItems; // Zoznam poloziek

        /// <summary>Property: Count</summary>
        public int Count
        {
            get
            {
                return FItems.Count;
            }
        }
        /// <summary>Property: []</summary>
        /// <param name="pIndex">pozicia</param>
        /// <returns>hodnota</returns>
        public string this[int pIndex]
        {
            get
            {
                if ((pIndex < 0) || (pIndex >= FItems.Count)) return "";
                return FItems[pIndex];
            }
        }

        /// <summary>Konstruktor</summary>
        public IHistoryList()
        {
            FItems = new System.Collections.Generic.List<string>();
        }
        /// <summary>Destruktor</summary>
        public void Dispose()
        {
            FItems = null;
        }

        /// <summary>Vymazanie obsahu</summary>
        public void Clear()
        {
            FItems.Clear();
        }
        /// <summary>Zaradenie do zoznamu</summary>
        /// <param name="pValue">hodnota</param>
        public void Add(string pValue)
        {
            if (string.IsNullOrWhiteSpace(pValue)) return; // Nezaradim prazdne

            for (int i = 0; i < FItems.Count; i++) // Hladam existujuci zaznam
            {
                if (string.Compare(FItems[i], pValue, true) == 0) // Nasiel som?
                {
                    FItems.RemoveAt(i);
                    FItems.Add(pValue); // Zaradim na koniec
                    return;
                }
            }
            if (FItems.Count >= Capacity) FItems.RemoveAt(0); // Je uz zoznam plny?
            FItems.Add(pValue); // Zaradim na koniec
        }

        /// <summary>Serializacia obsahu</summary>
        /// <param name="pEncode">priznak zakodovania</param>
        /// <returns>vysledok</returns>
        public string Serialize(bool pEncode = true)
        {
            System.Text.StringBuilder B = new System.Text.StringBuilder();
            foreach (string S in FItems)
            {
                B.Append(S); B.Append(Environment.NewLine);
            }
            return pEncode ? ICore.Base64Encode(B.ToString()) : B.ToString();
        }
        /// <summary>Deserializacia obsahu</summary>
        /// <param name="pData">serializovany obsah</param>
        /// <param name="pDecode">priznak dekodovania</param>
        public void Deserialize(string pData, bool pDecode = true)
        {
            try
            {
                string Data = pDecode ? ICore.Base64Decode(pData) : pData;
                using (System.IO.StringReader R = new System.IO.StringReader(Data)) // String reader
                {
                    while (true)
                    {
                        string Line = R.ReadLine();
                        if (Line == null) break; // Koniec?
                        if (string.IsNullOrWhiteSpace(Line)) continue; // Prazdny riadok?

                        FItems.Add(Line); // Zaradim
                        if (FItems.Count >= Capacity) break; // Uz je plno?
                    }
                }
            }
            catch // Chyba
            {
                Clear();
            }
        }
	}
}
